const api = require('../../utils/request.js');

Page({
  data: {
    entities: [],
    loading: true,
    errMsg: '',
    isCharacter: false,
    default_image_url: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.28G3L2JDuwXQGE2pQRDkKQHaHL?w=186&h=180&c=7&r=0&o=5&cb=thvnext&dpr=1.9&pid=1.7',
    title: ''
  },

  onLoad: function(options) {
    const id = options.id;
    const type = options.type; // 'character' 或 'person'
    console.log(options);
    this.setData({ isCharacter: type === 'character' });
    this.setData({ title: type === 'character' ? '角色列表' : '人物列表' });
    if (type === 'character') {
      this.loadCharacters(id);
    } else if (type === 'person') {
      this.loadPersons(id);
    }
  },

  loadCharacters: function(subjectId) {
    api.getSubjectCharacters(subjectId) // 这里需要替换为实际的搜索方法
      .then((res) => {
        this.setData({
          entities: res,
          loading: false
        });
      })
      .catch((err) => {
        console.error('Error loading characters:', err);
        this.setData({
          errMsg: '加载角色列表失败',
          loading: false
        });
      });
  },

  loadPersons: function(subjectId) {
    api.getSubjectPersons(subjectId) // 这里需要替换为实际的搜索方法
      .then((res) => {
        this.setData({
          entities: res,
          loading: false
        });
      })
      .catch((err) => {
        console.error('Error loading persons:', err);
        this.setData({
          errMsg: '加载人物列表失败',
          loading: false
        });
      });
  },

  jumpToDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    const type = this.data.isCharacter ? 'character' : 'person';
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}&type=${type}`,
    });
  }
});